package com.mhxy.service.impl;

import com.mhxy.constant.PicConstants;
import com.mhxy.service.AbstractTask;
import com.mhxy.util.FuzzyPicUtil;
import com.mhxy.util.ThreadUtil;
import com.sun.jna.platform.win32.WinDef;
import lombok.extern.slf4j.Slf4j;

import java.io.File;

@Slf4j
public class MapGetTaskImpl extends AbstractTask {

    private static final String 右侧任务栏宝图任务 = PicConstants.TASK_MAP_DIR + File.separator + "BaoTuRenWu_Right.PNG";
    private static final String 活动_宝图任务 = PicConstants.TASK_MAP_DIR + File.separator + "BaoTuRenWu_HuoDong.PNG";
    private static final String 活动_宝图任务完成 = PicConstants.TASK_MAP_DIR + File.separator + "BaoTuWanCheng.PNG";
    private static final String 宝图任务接受_听听无妨 = PicConstants.TASK_MAP_DIR + File.separator + "TingTingWuFang_BaoTu.PNG";


    public MapGetTaskImpl(WinDef.HWND hwnd_0) {
        super(hwnd_0);
    }

    @Override
    public String getTaskName() throws Exception {
        return "宝图任务";
    }

    @Override
    public Boolean before() throws Exception {
        clickEsc1();
        return goToJianYe();
    }

    @Override
    public Boolean execute() throws Exception {
        // 先检查任务列表是否存在，存在则直接点击开始任务
        if (findAndClickTaskList(右侧任务栏宝图任务)) {
            return true;
        }

        // 点击活动
        if (!findAndClickActivity()) {
            return false;
        }

        // 点击参加藏宝图任务
        Boolean canYuRes = findAndClickTaskTitle(活动_宝图任务, 活动_宝图任务完成);
        if (canYuRes == null || !canYuRes) {
            return canYuRes;
        }

        ThreadUtil.sleep(5000);

        // 点击接受任务
        if (!FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_0, 宝图任务接受_听听无妨,3, 0.8)) {
            return false;
        }

        // 点击任务列表开始任务
        for (int i = 0; i < 20; i++) {

            findAndClickTaskList(右侧任务栏宝图任务);

            ThreadUtil.sleep(5000);

        }

        return false;

    }

    @Override
    public Boolean after() throws Exception {
        int times = 0;
        while (times < 30) {
            times++;
            clickEsc1();
            if (FuzzyPicUtil.findPic(hwnd_0, PicConstants.公共_活动, 1).isFinded()) {
                // 双重查找,保证准确找到
                ThreadUtil.sleep(15000);
                clickEsc1();
                if (FuzzyPicUtil.findPic(hwnd_0, PicConstants.公共_活动, 1).isFinded()) {
                    log.info("【{}】任务结束", getTaskName());
                    return true;
                }
            }
            findBattle();
            log.info("【{}】战斗结束", getTaskName());
        }
        return true;
    }
}
